<template>
    <div class="sys-user-reg-way-container flex flex-col">
        <a-card class="mb-2.5">
            <common-form
                :model="state.queryForm"
                name="menu-form"
                @query="handleQuery"
                @add="handleAdd"
                @reset="handleReset"
            >
                <a-form-item
                    v-if="userInfoStore.state.userInfos.accountType == 999"
                    label="租户"
                    name="tenantId"
                    style="width: 250px;"
                >
                    <a-select v-model:value="state.queryForm.tenantId" placeholder="">
                        <a-select-option
                            v-for="item in state.tenantList"
                            :key="item.value"
                            :value="item.value">
                            {{item.label}}    
                        </a-select-option>
                    </a-select>
                </a-form-item>
                <a-form-item
                    label="关键字"
                    name="keyword"
                    class="w-1/2.5"
                >
                    <a-input v-model:value="state.queryForm.keyword"/>
                </a-form-item>
                <a-form-item
                    label="名称"
                    name="name"
                    class="w-1/2.5"
                >
                    <a-input v-model:value="state.queryForm.name"/>
                </a-form-item>
            </common-form>
        </a-card>
        <a-card class="flex-1 card-table">
            <common-table 
                :columns="columns" 
                :data="state.tableData"
                :pagination="{
                        total: state.tableParams.total,
                        current: state.tableParams.page,
                        pageSize: state.tableParams.pageSize,
                        showQuickJumper: true,
                        showSizeChanger: true,
                        pageSizeOptions:['2','5','10','20'],
                        showTotal:(total:number) => `总数: ${total}`
                }"
                :loading="state.loading"
                @change="changePagination"
            >
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex ==='history'">
                        <div style="cursor: pointer;">
                            <edit-record-history :row="record"/>
                        </div>
                    </template>
                    <template v-if="column.dataIndex === 'operation'">
                        <div class="w-full">
                            <a-button :icon="h(EditOutlined)" ghost type="primary" class="mr-1" @click="handleEdit(record)" >编辑</a-button>
                            <a-popconfirm
                                title="确定删除?"
                                @confirm="handleDelete(record)"
                            >
                                <a-button :icon="h(DeleteOutlined)" danger >删除</a-button>
                            </a-popconfirm>
                        </div>
                    </template>
                </template>
            </common-table>
        </a-card>
        <edit-reg-way ref="editRegWayRef" @handle-query="handleQuery"/>
    </div>
</template>


<script setup lang="ts" name="sysUserRegWay">
import { onMounted,reactive,h,ref } from 'vue';
import CommonForm from '@/components/common-form/index.vue'
import CommonTable from '@/components/common-table/index.vue'
import { useUserInfo } from '@/stores/userInfo';
import { apiSysTenantListGet } from '@/api/sys/sysTenant';
import type { ResType } from '@/api/types';
import type { TenantListType } from '../onlineUser/type';
import { DeleteOutlined, EditOutlined } from '@ant-design/icons-vue';
import { apiSysUserRegWayDeletePost, apiSysUserRegWayListPost } from '@/api/sys/SysUserRegWay';
import editRegWay from './components/editRegWay.vue';
import { showMsg } from '@/utils';

defineOptions({
    name: "sysUserRegWay",
})

const editRegWayRef = ref<InstanceType<typeof editRegWay>>()
const userInfoStore = useUserInfo()
const state = reactive({
    queryForm:{
        name: "",
		keyword: "",
		tenantId: undefined,
    } as any,
    tenantList:[] as TenantListType,
    tableData:[] as any[],
    tableParams: {
		page: 1,
		pageSize: 50,
		total: 0 as any,
	},
    loading:false,
})

const columns:any[] = [
    {
        title: '序号',
        align: "center",
        width: 60,
        customRender: ({ index }:{index:number}) => {
            return `${ index + 1 }`;
        },
        fixed: 'left',
    },
    {
        title: '名称',
        dataIndex: 'name',
        key: 'name',
        align: 'center',
        width: 100,
    },  
    {
        title: '机构',
        dataIndex: 'orgName',
        key: 'orgName',
        align: 'center',
        width: 150,
    }, 
    {
        title: '角色',
        dataIndex: 'roleName',
        key: 'roleName',
        align: 'center',
        width: 150,
    }, 
    {
        title: '职位',
        dataIndex: 'posName',
        key: 'posName',
        align: 'center',
        width: 150,
    }, 
    {
        title: '排序',
        dataIndex: 'orderNo',
        key: 'orderNo',
        align: 'center',
        width: 150,
    }, 
    {
        title: '修改记录',
        dataIndex: 'history',
        key: 'history',
        align: 'center',
        width: 150,
    }, 
    {
        title: '操作',
        align: 'center',
        key:"operation",
        dataIndex: 'operation',
        width: 250,
        fixed: 'right',
    }
]

const handleQuery = async () => {
    state.loading = true;
    let params = Object.assign(state.queryForm,state.tableParams)
    const res = await apiSysUserRegWayListPost({...params}) as ResType<any>
    state.tableData = res.data.result
    state.loading = false
}

const handleAdd = () => {
    editRegWayRef.value?.handleOpen({tenantId: state.queryForm.tenantId, orderNo: 100 })
}
const handleReset = () => {
    state.queryForm.name = "";
	state.queryForm.keyword = "";
	state.queryForm.tenantId = state.tenantList[0].value;
	handleQuery();
}
const handleDelete = async (record:any) => {
	await apiSysUserRegWayDeletePost({id:record.id})
    showMsg('删除成功','success')
    await handleQuery()	
}
const handleEdit = (record:any) => {
    editRegWayRef.value?.handleOpen(record)
}
const changePagination = async(pagination:{current:number,pageSize:number})=>{
    state.tableParams.page = pagination.current
    state.tableParams.pageSize = pagination.pageSize
    await handleQuery()
}

onMounted(async()=>{
    if (userInfoStore.state.userInfos.accountType === 999) {
        const res = await apiSysTenantListGet() as ResType<TenantListType>;
        state.tenantList = res.data.result;
        state.queryForm.tenantId = state.tenantList[0].value;
    }
    await handleQuery()
})
</script>


<style lang="scss" scoped>
.sys-user-reg-way-container{
    .card-table{
        :deep(.ant-card-body){
            height: 100%;
        }
    }
}
:deep(.ant-table-cell-fix-right){
    // right: 0 !important;
}
:deep(.ant-btn >span) {
    display:inline-flex!important;
}
</style>